function cpToolbarClass()
{
	
	//  ---------------------------------------
	//   Variables
	//  ---------------------------------------
	
	this.textarea = '';
	this.selecteurFormat = '';
	
	this.wikiToolbar = '';
	this.boutonsWiki = new Array();
	
	this.xhtmlToolbar = '';
	this.boutonsXHTML = new Array();
	
	this.avecTinymce = true;
	
	this.tinymce_initialise = false;
	this.tinymceCSS = '';
	
	//  ---------------------------------------
	//   Initialisation
	//  ---------------------------------------
	
	this.initialiser = function(textarea, selecteurFormat, css, entete)
	{
		
		var inst = this;
		
		// Initialisation des variables
		inst.textarea = $(textarea);
		inst.selecteurFormat = $(selecteurFormat);
		
		inst.tinymceCSS = css;
		inst.entete = entete;
		
		// Ignorer les navigateur incompatibles
		if (!(document.selection && document.selection.createRange)
			&& inst.textarea[0].selectionStart === null) {
			return false;
		}

		// Création de la wikiToolbar
		var wikiToolbarId = inst.textarea.attr('id')+'_wiki_toolbar';
		inst.textarea.before('<div id="'+wikiToolbarId+'" style="height:22px;"></div>').height(inst.textarea.height()-22);
		inst.wikiToolbar = $('#'+wikiToolbarId);
		inst.creerBoutonsWiki();
		
		// Création de la toolbar XHTML
		if(!this.avecTinymce)
		{
			var xhtmlToolbarId = inst.textarea.attr('id')+'_xhtml_toolbar';
			inst.textarea.before('<div id="'+xhtmlToolbarId+'" style="height:22px;"></div>');
			inst.xhtmlToolbar = $('#'+xhtmlToolbarId);
			inst.creerBoutonsXHTML();
		} else {
			inst.xhtmlToolbar = $(inst.textarea.attr('id')+'_xhtml_toolbar');
		}
		
		// Rafraîchir le format
		inst.rafraichirFormat();
		
		this.selecteurFormat.change(function() {
			inst.rafraichirFormat(inst)
		});
		
		return true;		
	}
	
	this.rafraichirFormat = function()
	{
		
		var format = this.selecteurFormat.val();
		
		// XHTML - tinymce
		if(format == '1' && this.avecTinymce) {
			this.initialiseTinymce();
		}
		else if(format == '1') {
			this.afficherXHTML();
		}
		// Wiki
		else {
			this.afficherWiki();
		}
		
	}
	
	//  ---------------------------------------
	//   XHTML - tinymce
	//  ---------------------------------------
	
	this.afficherTinymce = function(tinymce_inst)
	{
		var editorId = tinymce_inst.editorId;
		cpToolbars[editorId].wikiToolbar.hide();		
		$('#'+editorId).tinymce().show();
	};
	
	this.initialiseTinymce = function()
	{
		
		var inst = this;
		
		var textareaName = inst.textarea.attr('name');
		var height = inst.textarea.height()+15;
		
		var enteteOption = '';
		if(inst.entete) {
			enteteOption = 'pagebreak,';
		}

		if(this.tinymce_initialise == false)
		{

			$(inst.textarea).tinymce({
				
				script_url : 'js/tinymce/tiny_mce.js',
				
				mode : "exact",
				
				theme : "advanced",
				height: height,
				
				plugins : "cpEntete,cpGadget,cpLien,cpImage,cpMultimedia,cpTag,safari,style,table,emotions,searchreplace,print,contextmenu,paste,fullscreen,xhtmlxtras,inlinepopups",
				
				theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,sub,sup,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
				theme_advanced_buttons2 : "bullist,numlist,|,outdent,indent,blockquote,|,link,unlink,anchor,image,media,emotions,gadget,|,forecolor,backcolor,styleprops,attribs,|,"+enteteOption+"cpTag,|,fullscreen,code",
				theme_advanced_buttons3 : "",
				
				theme_advanced_toolbar_location : "top",
				theme_advanced_toolbar_align : "left",
				theme_advanced_statusbar_location : "bottom",
				theme_advanced_resizing : false,
				
				convert_urls : true,
				document_base_url : cpUrlPublique,
				relative_urls : false,
				remove_script_host : true,
				
				language : cpLangue,
				
				valid_elements : "@[id|class|style|title|dir<ltr?rtl|lang|xml::lang|onclick|ondblclick|"
					+ "onmousedown|onmouseup|onmouseover|onmousemove|onmouseout|onkeypress|"
					+ "onkeydown|onkeyup],a[rel|rev|charset|hreflang|tabindex|accesskey|type|"
					+ "name|href|target|title|class|onfocus|onblur],strong/b,em/i,strike,u,"
					+ "#p,-ol[type|compact|start],-ul[type|compact],-li,br,img[longdesc|usemap|"
					+ "src|border|alt=|title|hspace|vspace|width|height|align|options],-sub,-sup,"
					+ "-blockquote,-table[border=0|cellspacing|cellpadding|width|frame|rules|"
					+ "height|align|summary|bgcolor|background|bordercolor],-tr[rowspan|width|"
					+ "height|align|valign|bgcolor|background|bordercolor],tbody,thead,tfoot,"
					+ "#td[colspan|rowspan|width|height|align|valign|bgcolor|background|bordercolor"
					+ "|scope],#th[colspan|rowspan|width|height|align|valign|scope],caption,-div,"
					+ "-span,-code,-pre,address,-h1,-h2,-h3,-h4,-h5,-h6,hr[size|noshade],-font[face"
					+ "|size|color],dd,dl,dt,cite,abbr,acronym,del[datetime|cite],ins[datetime|cite],"
					+ "object[classid|width|height|codebase|*],param[name|value|_value],embed[type|width"
					+ "|height|src|*],script[src|type],map[name],area[shape|coords|href|alt|target],bdo,"
					+ "button,col[align|char|charoff|span|valign|width],colgroup[align|char|charoff|span|"
					+ "valign|width],dfn,fieldset,form[action|accept|accept-charset|enctype|method],"
					+ "input[accept|alt|checked|disabled|maxlength|name|readonly|size|src|type|value],"
					+ "kbd,label[for],legend,noscript,optgroup[label|disabled],option[disabled|label|selected|value],"
					+ "q[cite],samp,select[disabled|multiple|name|size],small,"
					+ "textarea[cols|rows|disabled|name|readonly],tt,var,big",
				entity_encoding : "raw",
					
				content_css : inst.tinymceCSS,
					init_instance_callback:inst.afficherTinymce
				
			});
			
			inst.tinymce_initialise = true;
			
		}
		else
		{
			this.afficherTinymce(tinyMCE['editors'][inst.textarea.attr('name')]);
		}
		
	}
	
	//  ---------------------------------------
	//   Wiki
	//  ---------------------------------------
	
	this.afficherWiki = function()
	{
		if(this.tinymce_initialise) {
			this.textarea.tinymce().hide();
		}
		
		this.xhtmlToolbar.hide();
		this.wikiToolbar.show();
	};
	
	this.creerBoutonsWiki = function()
	{
		var inst = this;
		
		this.definirBoutonWiki("images/wiki_toolbar/titre1.png", 50, 22, "Titre 1","!!!!","","Titre 1","cp-wikitoolbar-titre1");
		this.definirBoutonWiki("images/wiki_toolbar/titre2.png", 50, 22, "Titre 2","!!!","","Titre 2","cp-wikitoolbar-titre2");
		this.definirBoutonWiki("images/wiki_toolbar/titre3.png", 50, 22, "Titre 3","!!","","Titre 3","cp-wikitoolbar-titre3");
		this.definirBoutonWiki("images/wiki_toolbar/gras.png", 22, 22, "Texte gras","__","__","Texte gras","cp-wikitoolbar-gras");
		this.definirBoutonWiki("images/wiki_toolbar/italique.png", 22, 22, "Texte italique","''","''","Texte italique","cp-wikitoolbar-italique");
		this.definirBoutonWiki("images/wiki_toolbar/souligne.png", 22, 22, "Texte souligné","++","++","Texte souligné","cp-wikitoolbar-souligne");	
		
		for (var i = 0; i < this.boutonsWiki.length; i++) {
			this.creerBouton('wiki', this.boutonsWiki[i], function(inst, tagOpen, tagClose, sampleText) {
				inst.insererTag(tagOpen, tagClose, sampleText);
			});
		}
		
	};
	
	this.definirBoutonWiki = function(imageFile, imageWidth, imageHeight, speedTip, tagOpen, tagClose, sampleText, imageId)
	{
		this.boutonsWiki[this.boutonsWiki.length] =
			{"imageId": imageId,
			 "imageFile": imageFile,
			 "imageWidth": imageWidth,
			 "imageHeight": imageHeight,
			 "speedTip": speedTip,
			 "tagOpen": tagOpen,
			 "tagClose": tagClose,
			 "sampleText": sampleText
			};
	};
	

	//  ---------------------------------------
	//   XHTML
	//  ---------------------------------------
	
	this.afficherXHTML = function()
	{		
		this.wikiToolbar.hide();
		this.xhtmlToolbar.show();
	};
	
	this.creerBoutonsXHTML = function()
	{
		var inst = this;
		
		this.definirBoutonXHTML("images/wiki_toolbar/titre1.png", 50, 22, "Titre 1","<h3>","</h3>","Titre 1","cp-xhtmltoolbar-titre1");
		this.definirBoutonXHTML("images/wiki_toolbar/titre2.png", 50, 22, "Titre 2","<h4>","</h4>","Titre 2","cp-xhtmltoolbar-titre2");
		this.definirBoutonXHTML("images/wiki_toolbar/titre3.png", 50, 22, "Titre 3","<h5>","</h5>","Titre 3","cp-xhtmltoolbar-titre3");
		this.definirBoutonXHTML("images/wiki_toolbar/gras.png", 22, 22, "Texte gras","<strong>","</strong>","Texte gras","cp-xhtmltoolbar-gras");
		this.definirBoutonXHTML("images/wiki_toolbar/italique.png", 22, 22, "Texte italique","<em>","</em>","Texte italique","cp-xhtmltoolbar-italique");
		this.definirBoutonXHTML("images/wiki_toolbar/souligne.png", 22, 22, "Texte souligné","<span style=\"text-decoration:underline;\">","</span>","Texte souligné","cp-xhtmltoolbar-souligne");	
		
		for (var i = 0; i < this.boutonsXHTML.length; i++) {
			this.creerBouton('xhtml',this.boutonsXHTML[i], function(inst, tagOpen, tagClose, sampleText) {
				inst.insererTag(tagOpen, tagClose, sampleText);
			});
		}
		
	};
	
	this.definirBoutonXHTML = function(imageFile, imageWidth, imageHeight, speedTip, tagOpen, tagClose, sampleText, imageId)
	{
		this.boutonsXHTML[this.boutonsXHTML.length] =
			{"imageId": imageId,
			 "imageFile": imageFile,
			 "imageWidth": imageWidth,
			 "imageHeight": imageHeight,
			 "speedTip": speedTip,
			 "tagOpen": tagOpen,
			 "tagClose": tagClose,
			 "sampleText": sampleText
			};
	};
	
	//  ---------------------------------------
	//   Commun aux 2 toolbars
	//  ---------------------------------------
	
	this.creerBouton = function(toolbar, item, action) {
		
		var inst = this;
		
		var image = document.createElement("img");
		image.width = item.imageWidth;
		image.height = item.imageHeight;
		image.className = "boutonWikiToolbar";
		if (item.imageId) image.id = item.imageId;
		image.src = item.imageFile;
		image.border = 0;
		image.alt = item.speedTip;
		image.title = item.speedTip;
		image.style.cursor = "pointer";
		image.onclick = function() {
			action(inst, item.tagOpen, item.tagClose, item.sampleText);
			return false;
		};
		
		if(toolbar == 'wiki') {
			inst.wikiToolbar[0].appendChild(image);
		} else {
			inst.xhtmlToolbar[0].appendChild(image);
		}
		
		return true;
	};
	
	this.insererTag = function(tagOpen, tagClose, sampleText) {

		var txtarea = this.textarea[0];
		
		var selText, isSample = false;
	
		if (document.selection  && document.selection.createRange) { // IE/Opera
	
			//save window scroll position
			if (document.documentElement && document.documentElement.scrollTop)
				var winScroll = document.documentElement.scrollTop
			else if (document.body)
				var winScroll = document.body.scrollTop;
			//get current selection
			txtarea.focus();
			var range = document.selection.createRange();
			selText = range.text;
			//insert tags
			checkSelectedText();
			range.text = tagOpen + selText + tagClose;
			//mark sample text as selected
			if (isSample && range.moveStart) {
				if (window.opera)
					tagClose = tagClose.replace(/\n/g,'');
				range.moveStart('character', - tagClose.length - selText.length);
				range.moveEnd('character', - tagClose.length);
			}
			range.select();
			//restore window scroll position
			if (document.documentElement && document.documentElement.scrollTop)
				document.documentElement.scrollTop = winScroll
			else if (document.body)
				document.body.scrollTop = winScroll;
	
		} else if (txtarea.selectionStart || txtarea.selectionStart == '0') { // Mozilla
	
			//save textarea scroll position
			var textScroll = txtarea.scrollTop;
			//get current selection
			txtarea.focus();
			var startPos = txtarea.selectionStart;
			var endPos = txtarea.selectionEnd;
			selText = txtarea.value.substring(startPos, endPos);
			//insert tags
			checkSelectedText();
			txtarea.value = txtarea.value.substring(0, startPos)
				+ tagOpen + selText + tagClose
				+ txtarea.value.substring(endPos, txtarea.value.length);
			//set new selection
			if (isSample) {
				txtarea.selectionStart = startPos + tagOpen.length;
				txtarea.selectionEnd = startPos + tagOpen.length + selText.length;
			} else {
				txtarea.selectionStart = startPos + tagOpen.length + selText.length + tagClose.length;
				txtarea.selectionEnd = txtarea.selectionStart;
			}
			//restore textarea scroll position
			txtarea.scrollTop = textScroll;
		}
	
		function checkSelectedText(){
			if (!selText) {
				selText = sampleText;
				isSample = true;
			} else if (selText.charAt(selText.length - 1) == ' ') { //exclude ending space char
				selText = selText.substring(0, selText.length - 1);
				tagClose += ' ';
			}
		}
	
	};	
	
}

//  ---------------------------------------
//   cpToolbars
//  ---------------------------------------
cpToolbars = new Array();

//  ---------------------------------------
//   Intégration à jquery
//  ---------------------------------------

$.fn.cpToolbar = function(param_format_selector, param_css, param_entete) {
		
	return this.each(function() {
		obj = $(this);
		var toolbarId = obj.attr('id');
		cpToolbars[toolbarId] = new cpToolbarClass();

		cpToolbars[toolbarId].initialiser(obj, param_format_selector, param_css, param_entete);
	});

};

$.fn.cpMiniToolbar = function() {
	
	return this.each(function() {
		obj = $(this);

		$(obj).tinymce({
				
				script_url : 'js/tinymce/tiny_mce.js',
				theme : 'advanced',
				content_css : tinymceCSS,
				
				plugins : "cpLien,cpImage,safari,inlinepopups",
				
				theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,link,unlink,image,|,code",
				theme_advanced_buttons2 : "",
				theme_advanced_buttons3 : "",
				
				theme_advanced_toolbar_location : "top",
				theme_advanced_toolbar_align : "left",
				theme_advanced_resizing : false,
				
				convert_urls : true,
				document_base_url : cpUrlPublique,
				relative_urls : false,
				remove_script_host : true,
				
				language : cpLangue
				
		});
		
	});	
};

//---------------------------------------
//  Message quand l'utilisateur quitte la page
//---------------------------------------

$.fn.tinyMCEsignalerModifications = function() {
	
	return this.each(function() {
		
		obj = $(this);
		var toolbarId = obj.attr('id');
		
		if(cpToolbars[toolbarId].tinymce_initialise) {
			if(obj.tinymce().isDirty()) {
				cp.setPageModifiee();
			}
		}

	});

};

$.fn.tinyMCEisNotDirty = function() {
	
	return this.each(function() {
		
		obj = $(this);
		var toolbarId = obj.attr('id');
		
		if(cpToolbars[toolbarId].tinymce_initialise) {
			obj.tinymce().isNotDirty = 1;
		}

	});

};

$.fn.tinyMCEpreparerEnregistrement = function() {
	
	return this.each(function() {
		
		obj = $(this);
		var toolbarId = obj.attr('id');
		
		if(cpToolbars[toolbarId].tinymce_initialise && cpToolbars[toolbarId].selecteurFormat.val() == '1') {
			obj.val(obj.tinymce().getContent());
		}

	});

};